﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;

namespace FineUI.Examples.form
{
    public partial class tuijianxcf : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCheckBoxListZydl("本科");

            }
        }


        #region Bindtuijianxcf

        public class TestClass
        {
            private string _id;

            public string Id
            {
                get { return _id; }
                set { _id = value; }
            }
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            public TestClass(string id, string name)
            {
                _id = id;
                _name = name;
            }

        }

        /// <summary>
        /// 绑定专业大类
        /// </summary>
        private void BindCheckBoxListZydl(string ccmc)
        {
            System.Data.DataSet ds = new System.Data.DataSet();
            ds = BookDAL.SqlHelper.ExecuteDataset(BookDAL.SqlHelper.GetConnSting(), System.Data.CommandType.Text, "SELECT   [专业代码]   ,[专业名称]       FROM [专业库]  where LEN(专业代码)=2 and 层次=@ccmc", new SqlParameter("@ccmc", ccmc));
            //List<TestClass> myList = new List<TestClass>();
            //myList.Add(new TestClass("1", "数据绑定值 1"));
            //myList.Add(new TestClass("2", "数据绑定值 2"));
            //myList.Add(new TestClass("3", "数据绑定值 3"));
            //myList.Add(new TestClass("4", "数据绑定值 4"));

            zydl.DataTextField = "专业名称";
            zydl.DataValueField = "专业代码";
            zydl.DataSource = ds.Tables[0];
            zydl.DataBind();

            //CheckBoxList2.SelectedValueArray = new string[] { "1", "3" };
        }
        /// <summary>
        /// 绑定专业小类
        /// 大类in字符串形如('01','02')
        /// </summary>
        /// <param name="zydl">大类in字符串</param>
        private void BindCheckBoxListZyxl(string zydl)
        {
            //zydl="()"
            System.Data.DataSet ds = new System.Data.DataSet();
            if (zydl == "()")
                BookDAL.SqlHelper.FillDataset(BookDAL.SqlHelper.GetConnSting(), System.Data.CommandType.Text, "SELECT  [专业代码]   ,[专业名称]     FROM [专业库]  where 0>0", ds, new string[] { "专业库" });//不加载数据

            else
                BookDAL.SqlHelper.FillDataset(BookDAL.SqlHelper.GetConnSting(), System.Data.CommandType.Text, "SELECT  [专业代码]   ,[专业名称]     FROM [专业库]  where  len(专业代码)=4 and left(专业代码,2) in " + zydl, ds, new string[] { "专业库" });
            //List<TestClass> myList = new List<TestClass>();
            //myList.Add(new TestClass("1", "数据绑定值 1"));
            //myList.Add(new TestClass("2", "数据绑定值 2"));
            //myList.Add(new TestClass("3", "数据绑定值 3"));
            //myList.Add(new TestClass("4", "数据绑定值 4"));

            zyxl.DataTextField = "专业名称";
            zyxl.DataValueField = "专业代码";
            zyxl.DataSource = ds.Tables[0];
            zyxl.DataBind();

            //CheckBoxList2.SelectedValueArray = new string[] { "1", "3" };
        }
        #endregion

        #region Events
        protected void zydlAutoPostBack_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] tmp=zyxl.SelectedValueArray;
            BindCheckBoxListZyxl(GetArrayString2(zydl.SelectedValueArray));
            zyxl.SelectedValueArray = tmp;
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            //批线
            string nf = "2015";
            int pixian = (int)BookDAL.SqlHelper.ExecuteScalar(BookDAL.SqlHelper.GetConnSting(), System.Data.CommandType.Text, 
                "SELECT [分数]  FROM [批线表]  where 录取批次=@pici and 科类=@kelei and 年份=@nf",
                new SqlParameter("@pici", this.ddlpici.SelectedValue), new SqlParameter("@kelei", this.ddlkelei.SelectedValue), new SqlParameter("@nf", nf));
            int xiancha = Convert.ToInt32(txtcj.Text) - pixian;
            labResult.Text = string.Concat("你的线差为", xiancha.ToString(), "---相应的批线为：", pixian.ToString());
            SimpleForm1.Title = labResult.Text;
            //
            if (ddlpici.SelectedText.Substring(0, 2) == "本科")
                BindCheckBoxListZydl("本科");
            else
                BindCheckBoxListZydl("专科");
            BindCheckBoxListZyxl("()");

        }

        protected void btnServerSetSelectedValue_Click(object sender, EventArgs e)
        {
            if(sender.Equals(this.zybtnclean))
                foreach (CheckItem ci in zyxl.Items) ci.Selected = false;
            else
                foreach (CheckItem ci in CheckBoxList1.Items) ci.Selected = false;
            //System.Threading.Thread.Sleep(3000);
        }
        protected void btnServerSetAllSelectedValue_Click(object sender, EventArgs e)
        {
            if (sender.Equals(this.zybtnall))
                foreach (CheckItem ci in zyxl.Items) ci.Selected = true;
            else
                foreach (CheckItem ci in CheckBoxList1.Items)  ci.Selected = true;
            //System.Threading.Thread.Sleep(3000);
            


        }

        protected void btnServerGetSelectedValue_Click(object sender, EventArgs e)
        {
            if (CheckBoxList1.SelectedValueArray.Length > 0)
            {
                Alert.ShowInTop(String.Format("列表一选中项的值：{0}", GetArrayString(CheckBoxList1.SelectedValueArray)));
            }
            else
            {
                Alert.ShowInTop(String.Format("列表一没有选中项！"));
            }
        }



        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("请确认你的信息：<br/>你的科类");
            sb.Append(ddlkelei.SelectedText);
            sb.Append(",你选择推荐的批次：");
            sb.Append(ddlpici.SelectedText);
            sb.Append("你的成绩：");
            sb.Append(txtcj.Text);
            sb.Append("<br/>你选择学校所在地：");
            sb.Append(GetArrayString(this.CheckBoxList1.SelectedValueArray));
            sb.Append("<br/>你选择专业类别：");
            sb.Append(GetArrayString(this.zyxl.SelectedItemArray));
            System.Data.DataSet ds = new System.Data.DataSet();
            ds = BookDAL.SqlHelper.ExecuteDataset(BookDAL.SqlHelper.GetConnSting(), System.Data.CommandType.StoredProcedure, "智能推荐线差法",
                new SqlParameter("@pici", ddlpici.SelectedText),
                new SqlParameter("@kelei", ddlkelei.SelectedText),
                new SqlParameter("@fenshu", Convert.ToInt32(txtcj.Text)),
                new SqlParameter("@sheng", GetArrayString(CheckBoxList1.SelectedValueArray)),
                new SqlParameter("@zyxl", GetArrayString(zyxl.SelectedValueArray)),
                new SqlParameter("@nf", "2015")
            );
            SimpleForm1.Expanded = false;

            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();


        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            //批线
            string nf = "2015";
            System.Data.DataSet ds = new System.Data.DataSet();
            ds = BookDAL.SqlHelper.ExecuteDataset(BookDAL.SqlHelper.GetConnSting(), System.Data.CommandType.StoredProcedure, "智能推荐线差法",
                new SqlParameter("@pici", ddlpici.SelectedText),
                new SqlParameter("@kelei", ddlkelei.SelectedText),
                new SqlParameter("@fenshu", Convert.ToInt32(txtcj.Text)),
                new SqlParameter("@sheng", GetArrayString(CheckBoxList1.SelectedValueArray)),
                new SqlParameter("@zyxl", GetArrayString(zyxl.SelectedValueArray)),
                new SqlParameter("@nf", nf)
            );
            string filename = string.Concat(ddlkelei.SelectedText, ddlpici.SelectedText, txtcj.Text, "线差法的推荐结果");
            EmptyProjectNet40_FineUI.CommSub.ExcelExport(ds.Tables[0], filename, filename);
        }

        private string GetArrayString(string[] array)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string item in array)
            {
                sb.Append(item);
                sb.Append(",");
            }
            return sb.ToString().TrimEnd(',');
        }
        private string GetArrayString2(string[] array)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("(");
            foreach (string item in array)
            {
                sb.Append("'");
                sb.Append(item);
                sb.Append("'");
                sb.Append(",");
            }
            return sb.ToString().TrimEnd(',')+")";
        }
        private string GetArrayString(CheckItem[] array)
        {
            StringBuilder sb = new StringBuilder();
            foreach ( CheckItem item in array)
            {
                sb.Append(item.Text);
                sb.Append(",");
            }
            return sb.ToString().TrimEnd(',');
        }

        #endregion


    }
}